package com.example.mq.demo;

import com.example.mq.mqclient.Channel;
import com.example.mq.mqclient.Connection;
import com.example.mq.mqclient.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.ExchangeType;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class DemoProducer {
    public static void main(String[] args) throws IOException, InterruptedException {
        log.info("生产者启动");
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("43.142.48.195");
        factory.setHost("127.0.0.1");
        factory.setPort(18081);
        Connection connection = factory.newConnection();

        // 创建 Channel
        Channel channel = connection.createChannel();

        // 创建 交换机 和 队列
        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
        channel.queueDeclare("testQueue", true, false, false, null);

        // 创建消息并发送
        while (true) {
            Thread.sleep(1000);
            byte[] body = ("hello").getBytes();
            BasicProperties basicProperties = new BasicProperties();
            basicProperties.setDeliverMode(2); // 设置消息持久化
            boolean ok = channel.basicPublish("testExchange", "testQueue", basicProperties, body);
            log.info("消息投递完成！ok=" + ok);
        }
    }
}
